package cn.keduox.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.keduox.dao.CartDao;
import cn.keduox.entity.CartList;
import cn.keduox.entity.OrderExtm;
import cn.keduox.service.OrderService;
import cn.keduox.utils.Result;

/**
 * 订单控制层
 * @author 邓中亚
 *
 */
@WebServlet("/order.do")
public class OrderController extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("进入post");
		// 设置编码
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		String servletFlag = req.getParameter("servletFlag");
		if ("orderInput".equals(servletFlag)) {
			orderInput(req, resp);
		} else if ("addOrderPage".equals(servletFlag)) {
			addOrderPage(req, resp);
		}
		
	}


/**
 * 添加订单的页面选择
 * @param req
 * @param resp
 * @throws IOException 
 * @throws ServletException 
 */
	private void addOrderPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("开始添加订单");
		String userId=req.getParameter("userId");
		System.out.println(userId);
		//将id传给服务层和dao层判断该用户应该去哪个页面
		OrderExtm orderExtm=new OrderExtm();
		CartDao carDao=new CartDao();
		List<CartList> cartListList = new ArrayList<CartList>();
		OrderService orderService=new OrderService();
		Result result=orderService.addOrderPage(userId);
		if (result.getMessage().equals("goLogin")) {
			//非注册用户去登录
			req.getRequestDispatcher("/WEB-INF/pages/login.jsp").forward(req, resp);
		} else if(result.getMessage().equals("goExtm")){//Extm.jsp
			//已经是会员但是没有收货地址去填写收货地址
			req.getRequestDispatcher("/WEB-INF/pages/addAddress.jsp").forward(req, resp);
		}else if(result.getMessage().equals("goAddOrder")) {
			//已经是会员并且有收货地址先把收货地址和需要购买的商品显示出来
			
			orderExtm=orderService.findExtm(userId);//获取收货地址
			
			 cartListList = new ArrayList<CartList>();			
			cartListList=carDao.showCart(userId);//获取订单中商品详情
			System.out.println("长度为"+cartListList.size());
			req.setAttribute("cartListList", cartListList);
			req.setAttribute("orderExtm", orderExtm);
			req.getRequestDispatcher("/WEB-INF/pages/addOrder.jsp").forward(req, resp);//goAddOrder
		}
		
	}
	/**
	 * 内部跳转到订单信息输入页面
	 * @param req
	 * @param resp
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void orderInput(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.getRequestDispatcher("/WEB-INF/pages/orderPay.jsp").forward(req, resp);
		
	}

}
